const { goodsFormatError, errorRequest, errorType } = require('../consitan/err.type')
const validator = async (ctx, next) => {
    try {
        ctx.verifyParams({
            goodsName: { type: 'string', required: true },
            goodsPrice: { type: 'number', required: true },
            goodsNum: { type: 'number', required: true },
            goodsImg: { type: 'string', required: false, allowEmpty: true},
            goodsType: { type: 'string', required: true },
            onShopping: { type: 'string', required: true },
        })
    }catch (err) {
        console.error(err)
        goodsFormatError.result = err
        return ctx.app.emit('error', goodsFormatError, ctx)
    }
    await next()
}

const paramValidator = async (ctx, next) => {
    try {
        ctx.verifyParams({
            pageSize: { type: 'string', required: true },
            pageNum: { type: 'string', required: true }
        })
    } catch (err) {
        console.log(err)
        errorRequest.result = err
        return ctx.app.emit('error', errorRequest, ctx)
    }
    await next()
}

const statusValidator = async (ctx, next) => {
    try {
        ctx.verifyParams({
            id: { type: 'string', required: true },
            onShopping: { type: 'string', required: true }
        })
    } catch (err) {
        console.log(err)
        errorType.result = err
        return ctx.app.emit('error', errorType, ctx)

    }
    await next()
}
module.exports = {
    validator,
    paramValidator,
    statusValidator
}